﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using WAF.EntityFramework;
using CtpTrader.Logic.Entity;
using CtpTrader.Logic.Core.Common;

namespace CtpTrader.Logic.Core.Trade
{
    public class Position
    {
        /// <summary>
        /// 持仓单位
        /// </summary>
        public PositionUnit Unit { get; private set; }

        /// <summary>
        /// 持仓均价
        /// </summary>
        public double Price { get; private set; }

        /// <summary>
        /// 持仓总数
        /// </summary>
        public int Volume { get; private set; }


        /// <summary>
        /// 构造函数
        /// </summary>
        public Position(PositionUnit unit)
        {
            this.Unit   = unit;
            this.Price  = 0;
            this.Volume = 0;
        }

        /// <summary>
        /// 开仓
        /// </summary>
        /// <param name="tradeTime">开仓时间</param>
        /// <param name="price">开仓价格</param>
        /// <param name="volume">开仓数量</param>
        public void Open(double price, int volume)
        {
            //计算总的持仓均价（结果采取四舍六入法，保留两位小数）
            this.Price = Math.Round((this.Price * this.Volume + price * volume) / (this.Volume + volume), 2);

            //计算总的持仓总数
            this.Volume += volume;

            this.LogTrade(ActionType.Open, price, volume);
        }

        /// <summary>
        /// 平仓
        /// </summary>
        /// <param name="tradeTime">平仓时间</param>
        /// <param name="price">平仓价格</param>
        /// <param name="volume">平仓数量</param>
        public void Close(double price, int volume)
        {
            //计算持仓均价（结果采取四舍六入法，保留两位小数）
            this.Price = Math.Round((this.Price * this.Volume - price * volume) / (this.Volume - volume), 2);

            //计算持仓总数
            this.Volume -= volume;

            this.LogTrade(ActionType.Close, price, volume);
        }

        /// <summary>
        /// 记录交易历史
        /// </summary>
        /// <param name="actionType">开平标志</param>
        /// <param name="price">交易价格</param>
        /// <param name="volume">交易数量</param>
        private void LogTrade(ActionType actionType, double price, int volume)
        {
            TradeHistory history = new TradeHistory();

            history.TradeTime       = DateTime.Now.ToShortTimeString();
            history.Strategy        = this.Unit.Strategy;
            history.Account         = this.Unit.Account.Number;
            history.InstrumentID    = this.Unit.Instrument.InstrumentID;
            history.Level           = this.Unit.BarUnit.Level;
            history.TimeSpan        = (int)this.Unit.BarUnit.TimeSpan;
            history.Direction       = (int)this.Unit.DirectionType;
            history.Action          = (int)actionType;
            history.Price           = price;
            history.Volume          = volume;

            EntityAction.Insert(history);
        }
    }
}
